<?php
/**
 * Created by PhpStorm.
 * User: lidan
 * Date: 2018/10/15
 * Time: 下午12:35
 */

namespace Imooc;


class AllUser implements \Iterator
{
    protected $ids;
    protected $data = array();
    protected $index;
    function __construct()
    {
        $db = Factory::getDatabase();
        $result = $db->query("select id from user");
        $this->ids = $result->fetch_all(MYSQLI_ASSOC);
    }

    /**
     * @1st
     */
    public function rewind()
    {
        $this->index = 0;
    }

    /**
     * @2nd
     * @return bool
     */
    public function valid()
    {
        return $this->index < count($this->ids) ;
    }

    /**
     * @3rd
     * @return User|mixed
     */
    public function current()
    {
        $id = $this->ids[$this->index]['id'];
        return Factory::getUser($id);
    }

    /**
     * @4th
     */
    public function next()
    {
        $this->index++;
    }

    /**
     * @5th
     * @return mixed
     */
    public function key()
    {
        return $this->index;
    }

}